package com.edu.zhku.wms.controller;

import com.edu.zhku.wms.common.CommonResult;
import com.edu.zhku.wms.common.Update;
import com.edu.zhku.wms.pojo.Employee;
import com.edu.zhku.wms.service.EmployeeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "员工管理")
@RestController
@RequestMapping("/api/employee")
public class EmployeeController {

    @Autowired
    EmployeeServiceImpl employeeService;

    @ApiOperation("根据主键查询单个员工信息")
    @Secured({"管理员", "财务"})
    @GetMapping("/selectEmployeeByPrimaryKey")
    public CommonResult selectEmployeeByPrimaryKey(Integer id) {

        Employee employee = employeeService.selectByPrimaryKey(id);

        if (employee != null) {
            return CommonResult.success(employee);
        }

        return CommonResult.error("查询不到员工");
    }

    @ApiOperation("根据员工的姓名、工号或岗位模糊查询员工信息")
    @Secured({"管理员", "财务"})
    @GetMapping("/selectEmployee")
    public CommonResult selectEmployee(String keyword,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return CommonResult.success(employeeService.list(keyword, pageNum, pageSize));
    }

    @ApiOperation("添加单个员工")
    @Secured("管理员")
    @PostMapping("/addEmployee")
    public CommonResult addEmployee(@RequestBody @Validated Employee employee) {

        if (employeeService.addEmploy(employee) > 0) {
            return CommonResult.success();
        }

        return CommonResult.error("添加员工失败");
    }

    @ApiOperation("根据主键移除单个员工信息")
    @Secured("管理员")
    @DeleteMapping("/deleteEmployeeByPrimaryKey")
    public CommonResult deleteEmployeeByPrimaryKey(Integer id) {

        if (employeeService.deleteEmployByPrimaryKey(id) > 0) {
            return CommonResult.success();
        }

        return CommonResult.error("移除员工信息失败");
    }

    @ApiOperation("根据主键修改单个员工信息")
    @Secured("管理员")
    @PutMapping("/updateEmployee")
    public CommonResult updateEmployee(@RequestBody @Validated(Update.class) Employee employee) {

        if (employeeService.updateEmploy(employee) > 0) {
            return CommonResult.success();
        }

        return CommonResult.error("修改员工信息出错");
    }
}
